﻿using CloudFranceTriple.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace CloudFranceTriple.Tests.Areas.Forum
{
    [TestClass]
    public class PrivateMessageControllerTest : UnitTestBase
    {
        private Int32 _unitPrivateMessageId = 21;

        [TestMethod]
        [TestCategory("PrivateMessage")]
        public void PrivateMessageController_Index()
        {
            // Arrange
            var controller = new CloudFranceTriple.WebRole.Areas.Forum.Controllers.PrivateMessageController(GetWebSecurity.Object);

            controller.ControllerContext = base.GetControllerContext.Object;

            var context =
                new Mock<HttpContextBase>(MockBehavior.Strict);

            controller.Url =
                new UrlHelper(new RequestContext(context.Object,
                new RouteData()), new RouteCollection());

            // Act
            var result = controller.Index() as ViewResult;

            // Assert
            Assert.IsNotNull(result);
        }

        [TestMethod]
        [TestCategory("PrivateMessage")]
        public void PrivateMessageController_Create_Post_ModelIsValid()
        {
            // Arrange
            var controller = new CloudFranceTriple.WebRole.Areas.Forum.Controllers.PrivateMessageController(GetWebSecurity.Object);

            controller.ControllerContext = base.GetControllerContext.Object;

            var context =
                new Mock<HttpContextBase>(MockBehavior.Strict);

            controller.Url =
                new UrlHelper(new RequestContext(context.Object,
                new RouteData()), new RouteCollection());

            var _now = DateTime.UtcNow;
            var msg = new PrivateMessageBoxSet
            {
                ToPrivateMessage = UserId,
                MessageBody_Author = "UnitTesteur",
                MessageBody_AuthorAdressIp = "::1",
                MessageBody_CreationDate = DateTime.UtcNow,
                MessageBody_Label = "Unit Test Private Message",
                MessageBody_Message = "Unit Test message",
                ParentId = null,
            };

            // Act
            var result = controller.Create(msg) as RedirectToRouteResult;
            UnitTestPrivateMessageId = msg.Id;

            // Assert
            Assert.IsNotNull(result);
        }

        [TestMethod]
        [TestCategory("PrivateMessage")]
        public void PrivateMessageController_CreateWithReply_Post_ModelIsValid()
        {
            // Arrange
            var controller = new CloudFranceTriple.WebRole.Areas.Forum.Controllers.PrivateMessageController(GetWebSecurity.Object);

            var _controllerContext = base.GetControllerContext;

            controller.ControllerContext = _controllerContext.Object;
            var context =
                new Mock<HttpContextBase>(MockBehavior.Strict);

            controller.Url =
                new UrlHelper(new RequestContext(context.Object,
                new RouteData()), new RouteCollection());

            var _now = DateTime.UtcNow;
            var msg = new PrivateMessageBoxSet
            {
                ToPrivateMessage = UserId,
                MessageBody_Author = "UnitTesteur",
                MessageBody_AuthorAdressIp = "::1",
                MessageBody_CreationDate = DateTime.UtcNow,
                MessageBody_Label = "Unit Test Private Message",
                MessageBody_Message = "Unit Test message",
                ParentId = UnitTestPrivateMessageId,
            };


            // Act
            var result = controller.Create(msg) as RedirectToRouteResult;
            UnitTestPrivateMessageId2 = msg.Id;

            // Assert
            Assert.IsNotNull(result);
        }

        [TestMethod]
        [TestCategory("PrivateMessage")]
        public void PrivateMessageController_MsgToRead()
        {
            // Arrange
            var controller = new CloudFranceTriple.WebRole.Areas.Forum.Controllers.PrivateMessageController(GetWebSecurity.Object);

            controller.ControllerContext = base.GetControllerContext.Object;

            var context =
                new Mock<HttpContextBase>(MockBehavior.Strict);

            controller.Url =
                new UrlHelper(new RequestContext(context.Object,
                new RouteData()), new RouteCollection());

            // Act
            var result = controller.MsgToRead() as PartialViewResult;

            // Assert
            Assert.IsNotNull(result);
        }

        [TestMethod]
        [TestCategory("PrivateMessage")]
        public void PrivateMessageController_PrivateMessageSend()
        {
            // Arrange
            var controller = new CloudFranceTriple.WebRole.Areas.Forum.Controllers.PrivateMessageController(GetWebSecurity.Object);

            controller.ControllerContext = base.GetControllerContext.Object;

            var context =
                new Mock<HttpContextBase>(MockBehavior.Strict);

            controller.Url =
                new UrlHelper(new RequestContext(context.Object,
                new RouteData()), new RouteCollection());

            // Act
            var result = controller.PrivateMessageSend() as PartialViewResult;

            // Assert
            Assert.IsNotNull(result);
        }

        [TestMethod]
        [TestCategory("PrivateMessage")]
        public void PrivateMessageController_PrivateMessageReceived()
        {
            // Arrange
            var controller = new CloudFranceTriple.WebRole.Areas.Forum.Controllers.PrivateMessageController(GetWebSecurity.Object);

            controller.ControllerContext = base.GetControllerContext.Object;

            var context =
                new Mock<HttpContextBase>(MockBehavior.Strict);

            controller.Url =
                new UrlHelper(new RequestContext(context.Object,
                new RouteData()), new RouteCollection());

            // Act
            var result = controller.PrivateMessageReceived() as PartialViewResult;

            // Assert
            Assert.IsNotNull(result);
        }

        [TestMethod]
        [TestCategory("PrivateMessage")]
        public void PrivateMessageController_Details()
        {
            // Arrange
            var controller = new CloudFranceTriple.WebRole.Areas.Forum.Controllers.PrivateMessageController(GetWebSecurity.Object);

            controller.ControllerContext = base.GetControllerContext.Object;

            var context =
                new Mock<HttpContextBase>(MockBehavior.Strict);

            controller.Url =
                new UrlHelper(new RequestContext(context.Object,
                new RouteData()), new RouteCollection());

            // Act
            var result = controller.Details(_unitPrivateMessageId) as ViewResult;

            // Assert
            Assert.IsNotNull(result);
        }

        [TestMethod]
        [TestCategory("PrivateMessage")]
        public void PrivateMessageController_Delete_Get()
        {
            // Arrange
            var controller = new CloudFranceTriple.WebRole.Areas.Forum.Controllers.PrivateMessageController(GetWebSecurity.Object);

            controller.ControllerContext = base.GetControllerContext.Object;

            var context =
                new Mock<HttpContextBase>(MockBehavior.Strict);

            controller.Url =
                new UrlHelper(new RequestContext(context.Object,
                new RouteData()), new RouteCollection());

            // Act
            var result = controller.Delete(_unitPrivateMessageId) as ViewResult;

            // Assert
            Assert.IsNotNull(result);
        }

        [TestMethod]
        [TestCategory("PrivateMessage")]
        public void PrivateMessageController_DeleteConfirmed_Post()
        {
            // Arrange
            var controller = new CloudFranceTriple.WebRole.Areas.Forum.Controllers.PrivateMessageController(GetWebSecurity.Object);

            controller.ControllerContext = base.GetControllerContext.Object;

            var context =
                new Mock<HttpContextBase>(MockBehavior.Strict);

            controller.Url =
                new UrlHelper(new RequestContext(context.Object,
                new RouteData()), new RouteCollection());

            // Act
            var result = controller.DeleteConfirmed(UnitTestPrivateMessageId) as RedirectToRouteResult;

            // Assert
            Assert.IsNotNull(result);

            result = controller.DeleteConfirmed(UnitTestPrivateMessageId2) as RedirectToRouteResult;

            // Assert
            Assert.IsNotNull(result);
        }
    }
}
